package com.example.springboot.controller;

import com.example.springboot.pojo.trade;
import com.example.springboot.service.TradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class TestController {
    @Autowired
    private TradeService tradeService;
    @RequestMapping("/dd")
    public String index3(Integer pageNumber, Model model) {
        int spPage = 0;
        int pageSize = 8;
        int pageNo = 1;
//        if (spPage == null) {
//            pageNo = 1;
//        } else {
//            pageNo = Integer.parseInt(spPage);
//            if (pageNo < 1) {
//                pageNo = 1;
//            }
//        }
        if(pageNumber == null || pageNumber <= 0){
            spPage = 1;
        }
        else{
            spPage = pageNumber;
        }
        int totalCount = 0;
        int count = tradeService.sumTrade();
        if (count > 0) {
            totalCount = count;
        }
        int maxPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        if (pageNo > maxPage) {
            pageNo = maxPage;
        }
        List<trade> list = tradeService.pagesearch(spPage - 1, pageSize);
        //最后把信息放入model转发到页面把信息带过去
        model.addAttribute("list", list);
        model.addAttribute("pageNo", pageNo);
        model.addAttribute("totalCount", totalCount);
        model.addAttribute("maxPage", maxPage);
        model.addAttribute("stpage", spPage);
        return "订单";
    }
    @RequestMapping("/jj")
    public String index4(){
        return "121";
    }
}
